package cn.com.cetccst.mdm.engine;

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.os.UserManager;

import com.hihonor.android.app.admin.DeviceApplicationManager;
import com.hihonor.android.app.admin.DeviceCameraManager;
import com.hihonor.android.app.admin.DeviceControlManager;
import com.hihonor.android.app.admin.DevicePackageManager;
import com.hihonor.android.app.admin.DeviceRestrictionManager;
import com.hihonor.android.app.admin.DeviceSettingsManager;
import com.hihonor.android.app.admin.DeviceTelephonyManager;
import com.hihonor.android.app.admin.DeviceVpnManager;

import java.util.List;

public class HonorMdmEngine extends BaseMDMEngine {
    private static final String TAG = "HonorMdmEngine";

    private DeviceApplicationManager deviceApplicationManager;
    private DevicePackageManager devicePackageManager;
    private DeviceSettingsManager settingsManager;
    private DeviceRestrictionManager restrictionManager;
    private DeviceCameraManager cameraManager;
    private DeviceVpnManager vpnManager;
    private DeviceTelephonyManager telephonyManager;
    private DeviceControlManager deviceControlManager;
    private DevicePolicyManager dpm;
    protected UserManager userManager;


    public HonorMdmEngine(Context context) {
        super(context);
        deviceApplicationManager = new DeviceApplicationManager();
        settingsManager = new DeviceSettingsManager();
        restrictionManager = new DeviceRestrictionManager();
        cameraManager = new DeviceCameraManager();
        vpnManager = new DeviceVpnManager();
        devicePackageManager = new DevicePackageManager();
        telephonyManager = new DeviceTelephonyManager();
        deviceControlManager = new DeviceControlManager();
        dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
        userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);


    }

    @Override
    public boolean isActiveDeviceAdmin() {
        boolean ret = false;
        try {
            ret = deviceControlManager.isForcedActiveDeviceAdmin(adminComponent);
        } catch (Exception e) {
        }
        return ret;
    }

    @Override
    public boolean setActiveDeviceAdmin(Activity activity) {
        boolean ret = false;
        try {
            ret = deviceControlManager.setForcedActiveDeviceAdmin(adminComponent, activity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

    @Override
    public void controlBluetooth(boolean disabled) {
        if (disabled) {
            if (!restrictionManager.isBluetoothDisabled(adminComponent)) {
                restrictionManager.setBluetoothDisabled(adminComponent, true);
                logger.log("restrict blue tooth via HONOR API");
            }
        } else {
            if (restrictionManager.isBluetoothDisabled(adminComponent)) {
                restrictionManager.setBluetoothDisabled(adminComponent, false);
                logger.log("release blue tooth via HONOR API");
            }
        }
    }

    @Override
    public boolean isBluetoothControlled() {
        return restrictionManager.isBluetoothDisabled(adminComponent);
    }

    @Override
    public void controlCamera(boolean disabled) {
        if (disabled) {
            if (!cameraManager.isVideoDisabled(adminComponent)) {
                cameraManager.setVideoDisabled(adminComponent, true);
                logger.log("restrict camera via HONOR API");
            }
        } else {
            if (cameraManager.isVideoDisabled(adminComponent)) {
                cameraManager.setVideoDisabled(adminComponent, false);
                logger.log("release camera via HONOR API");
            }
        }
        if (disabled) {
            if (!dpm.getCameraDisabled(adminComponent)) {
                dpm.setCameraDisabled(adminComponent, true);
                logger.log("restrict camera to disable via device admin.");
            }
        } else {
            if (dpm.getCameraDisabled(adminComponent)) {
                dpm.setCameraDisabled(adminComponent, false);
                logger.log("release camera to enable via device admin.");
            }
        }
    }

    @Override
    public boolean isCameraControlled() {
        return cameraManager.isVideoDisabled(adminComponent);
    }

    @Override
    public void controlWifi(boolean disabled) {
        if (disabled) {
            if (!restrictionManager.isWifiDisabled(adminComponent)) {
                restrictionManager.setWifiDisabled(adminComponent, true);
                logger.log("restrict wifi via HONOR API");
            }
        } else {
            if (restrictionManager.isWifiDisabled(adminComponent)) {
                restrictionManager.setWifiDisabled(adminComponent, false);
                logger.log("release wifi via HONOR API");
            }
        }
    }

    @Override
    public boolean isWifiControlled() {
        return restrictionManager.isWifiDisabled(adminComponent);
    }

    @Override
    public void controlMicroPhone(boolean disabled) {
        if (disabled) {
            if (!restrictionManager.isMicrophoneDisabled(adminComponent)) {
                restrictionManager.setMicrophoneDisabled(adminComponent, true);
                logger.log("restrict micro phone via HONOR API");
            }
        } else {
            if (restrictionManager.isMicrophoneDisabled(adminComponent)) {
                restrictionManager.setMicrophoneDisabled(adminComponent, false);
                logger.log("release micro phone via HONOR API");
            }
        }
    }

    @Override
    public boolean isMicroPhoneControlled() {
        return restrictionManager.isMicrophoneDisabled(adminComponent);
    }

    @Override
    public void controlExternalStorage(boolean disabled) {
        if (disabled) {
            if (!restrictionManager.isExternalStorageDisabled(adminComponent)) {
                restrictionManager.setExternalStorageDisabled(adminComponent, true);
                logger.log("restrict external storage via HONOR API");
            }
        } else {
            if (restrictionManager.isExternalStorageDisabled(adminComponent)) {
                restrictionManager.setExternalStorageDisabled(adminComponent, false);
                logger.log("release external storage via HONOR API");
            }
        }
    }

    @Override
    public boolean isExternalStorageControlled() {
        return restrictionManager.isExternalStorageDisabled(adminComponent);
    }

    @Override
    public void controlScreenShot(boolean disabled) {
        if (disabled) {
            if (!restrictionManager.isScreenCaptureDisabled(adminComponent)) {
                restrictionManager.setScreenCaptureDisabled(adminComponent, true);
                logger.log("restrict screen capture via HONOR API");
            }
        } else {
            if (restrictionManager.isScreenCaptureDisabled(adminComponent)) {
                restrictionManager.setScreenCaptureDisabled(adminComponent, false);
                logger.log("release screen capture via HONOR API");
            }
        }
    }

    @Override
    public boolean isScreenShotControlled() {
        return restrictionManager.isScreenCaptureDisabled(adminComponent);
    }

    @Override
    public void controlNFC(boolean disabled) {
        if (disabled) {
            if (!restrictionManager.isNFCDisabled(adminComponent)) {
                restrictionManager.setNFCDisabled(adminComponent, true);
                logger.log("restrict nfc via HONOR API");
            }
        } else {
            if (restrictionManager.isNFCDisabled(adminComponent)) {
                restrictionManager.setNFCDisabled(adminComponent, false);
                logger.log("release nfc via HONOR API");
            }
        }
    }

    @Override
    public boolean isNFCControlled() {
        return restrictionManager.isNFCDisabled(adminComponent);
    }

    @Override
    public void controlMobileHotSpot(boolean disabled) {
        if (disabled) {
            if (!restrictionManager.isWifiApDisabled(adminComponent)) {
                restrictionManager.setWifiApDisabled(adminComponent, true);
                logger.log("restrict wifi ap via HONOR API");
            }
        } else {
            if (restrictionManager.isWifiApDisabled(adminComponent)) {
                restrictionManager.setWifiApDisabled(adminComponent, false);
                logger.log("release wifi ap via HONOR API");
            }
        }
    }

    @Override
    public boolean isMobileHotSpotControlled() {
        return restrictionManager.isWifiApDisabled(adminComponent);
    }

    @Override
    public void controlCallout(boolean disabled) {
        if (disabled) {
            if (!restrictionManager.isVoiceDisabled(adminComponent)) {
                restrictionManager.setVoiceDisabled(adminComponent, true);
                logger.log("restrict outgoing call via HONOR API");
            }
        } else {
            if (restrictionManager.isVoiceDisabled(adminComponent)) {
                restrictionManager.setVoiceDisabled(adminComponent, false);
                logger.log("release outgoing call via HONOR API");
            }
        }
    }

    @Override
    public boolean isCalloutControlled() {
        return restrictionManager.isVoiceDisabled(adminComponent);
    }

    @Override
    public void controlSms(boolean disabled) {
        if (disabled) {
            if (!restrictionManager.isSMSDisabled(adminComponent)) {
                restrictionManager.setSMSDisabled(adminComponent, true);
                logger.log("restrict sms via HONOR API");
            }
        } else {
            if (restrictionManager.isSMSDisabled(adminComponent)) {
                restrictionManager.setSMSDisabled(adminComponent, false);
                logger.log("release sms via HONOR API");
            }
        }
    }

    @Override
    public boolean isSmsControlled() {
        return restrictionManager.isSMSDisabled(adminComponent);
    }

    @Override
    public void controlMobileData(boolean disabled) {
        String key = UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS;
        if (disabled) {
            if (!userManager.hasUserRestriction(key)) {
                dpm.addUserRestriction(adminComponent, key);
                logger.log(key + " user restriction has been added via Owner Engine");
            }
        } else {
            if (userManager.hasUserRestriction(key)) {
                dpm.clearUserRestriction(adminComponent, key);
                logger.log(key + " user restriction has been removed via Owner Engine");
            }
        }
    }

    @Override
    public boolean isMobileDataControlled() {
        String key = UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS;
        return userManager.hasUserRestriction(key);
    }

    @Override
    public void controlVolumeDown(boolean disabled) {
        if (disabled) {
            if (!restrictionManager.isVolumeAdjustDisabled(adminComponent)) {
                restrictionManager.setVolumeAdjustDisabled(adminComponent, true);
                logger.log("restrict volume adjust via HONOR API");
            }
        } else {
            if (restrictionManager.isVolumeAdjustDisabled(adminComponent)) {
                restrictionManager.setVolumeAdjustDisabled(adminComponent, false);
                logger.log("release volume adjust via HONOR API");
            }
        }
    }

    @Override
    public boolean isVolumeDownControlled() {
        return false;
    }

    @Override
    public void controlAutoTime(boolean disabled) {

    }

    @Override
    public boolean isAutoTimeControlled() {
        return false;
    }

    @Override
    public void controlUsbData(boolean disabled) {

    }

    @Override
    public boolean isUsbDataControlled() {
        return false;
    }

    @Override
    public void controlUsbDebug(boolean disabled) {

    }

    @Override
    public boolean isUsbDebugControlled() {
        return false;
    }

    @Override
    public void controlRestoreFactory(boolean disabled) {

    }

    @Override
    public boolean isRestoreFactoryControlled() {
        return false;
    }

    @Override
    public void controlSystemUpgrade(boolean disabled) {

    }

    @Override
    public boolean isSystemUpgradeControlled() {
        return false;
    }

    @Override
    public void controlGPS(boolean disabled) {

    }

    @Override
    public boolean setDisallowRunningApps(List<String> packageNames, boolean isBlock) {
        return false;
    }

    @Override
    public List<String> getDisallowRunningApps() {
        return null;
    }

    @Override
    public void installPackage(String apkPath) {

    }

    @Override
    public void uninstallPackage(String packageName) {

    }


    @Override
    public void formatSDCard() {

    }

    @Override
    public void factoryReset() {

    }

    @Override
    public boolean setInstallSourceApps(List<String> packageNames) {
        return false;
    }

    @Override
    public List<String> getInstallSourceApps() {
        return null;
    }

    @Override
    public boolean setNetworkWhiteListApps(List<String> packageNames) {
        return false;
    }

    @Override
    public List<String> getNetworkWhiteListApps() {
        return null;
    }

    @Override
    public boolean setKeepLiveApps(List<String> packageNames) {
        return false;
    }

    @Override
    public List<String> getKeepLiveApps() {
        return null;
    }

    @Override
    public boolean setSuperApps(List<String> packageNames) {
        return false;
    }

    @Override
    public List<String> getSuperApps() {
        return null;
    }

    @Override
    public boolean setDevicePassword(String password) {
        return false;
    }

    @Override
    public String getName() {
        return "Honor";
    }

}
